import { IFormProps, size, formItemLayout, getUploadResult, productPage, getImages, getFileList } from '../../utils/common';
import React from 'react';
import { Modal, Form, Input, Select, Upload, Checkbox } from 'antd';
import * as api from '../../api/api';
import { FormComponentProps } from 'antd/lib/form';
import Uploads from '../Uploads';
const TextArea = Input.TextArea;
export interface IItemCategoryEditProps extends FormComponentProps {
    visible: boolean,
    itemCategory?: api.SingleUserMallItemsVOItemCategoryVO,
    onSave: (any) => void,
    onCancel: () => void,
    itemCategories: Array<api.SingleUserMallItemsVOItemCategoryVO>,
    businessScope?:api.SingleUserMallItemsVOBusinessScopeVO,
    businessScopes: Array<api.SingleUserMallItemsVOBusinessScopeVO>,
}
class ItemCategoryEdit extends React.PureComponent<IItemCategoryEditProps, any>{
    constructor(props) {
        super(props);
    }
    state = {
        fileList: [],
        pictureList:[]
    }

    // componentWillReceiveProps(nextProps) {
    //     const images = nextProps.itemCategory && nextProps.itemCategory.picture;
    //     this.setState({ fileList: getFileList(images) });
    // }
    componentDidMount() {
        const images = this.props.itemCategory && this.props.itemCategory.picture;
        this.setState({ fileList: getFileList(images) });
    }

    ok = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, form) => {
            if (!err) {
                form['picture'] = getImages(this.state.fileList);
                this.props.onSave(form);
            }
        });
    }

    cancel = () => {
        this.props.onCancel();
    }

    imageChange = (fileList) => {
        this.setState({ fileList });
    }

    pictureChange = (pictureList) => {
        this.setState({ pictureList });
    }

    render(): JSX.Element {
        const { getFieldDecorator } = this.props.form;
        const { itemCategory, itemCategories,businessScope,businessScopes } = this.props;
        return (
            <Modal
                title='商品分类信息'
                visible={this.props.visible}
                onOk={this.ok}
                onCancel={this.cancel}
                maskClosable={true}
            >
                <Form>
                    <Form.Item label="上级分类" {...formItemLayout}>
                        {getFieldDecorator('parentId', {
                            initialValue: itemCategory && itemCategory.parentId
                        })(<Select style={{ width: 120 }}>
                            <Select.Option value=''>全部</Select.Option>
                            {
                                itemCategories.map((item: api.SingleUserMallItemsVOItemCategoryVO) => {
                                    return <Select.Option value={item.id}>{item.name}</Select.Option>
                                })
                            }
                        </Select>)}
                    </Form.Item>
                    <Form.Item label="分类名称" {...formItemLayout}>
                        {getFieldDecorator('name', {
                            rules: [
                                {
                                    required: true,
                                    message: '请输入分类名称'
                                }
                            ],
                            initialValue: itemCategory && itemCategory.name
                        })(<Input size={size}
                            placeholder="请输入分类名称" />)}
                    </Form.Item>
               
                    <Form.Item label="分类图片" {...formItemLayout}>
                        <Uploads editable={true} fileList={this.state.fileList} onChange={this.imageChange} />
                    </Form.Item>
                    <Form.Item label="是否启用" {...formItemLayout}>
                        {getFieldDecorator('enabled', {
                            valuePropName: 'checked',
                            initialValue: itemCategory && itemCategory.enabled
                        })(<Checkbox />)}
                    </Form.Item>
                </Form>
            </Modal>)
    }
}

export default Form.create()(ItemCategoryEdit);